﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using edu.Model;
using Discuz.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;

namespace edu.Service
{

    public class StudentService
    {
        public static int Add(Student entity)
        {
            int id;
            string sql = string.Format("insert into [edu_student]([Code],[Name],[Description],[ClassId],[PatriarchId]) values(@Code,@Name,@Description,@ClassId,@PatriarchId)");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Code", Value=entity.Code},
                new SqlParameter(){DbType=DbType.String, ParameterName="Name", Value=entity.Name},
                new SqlParameter(){DbType=DbType.String, ParameterName="Description", Value=entity.Description},
                new SqlParameter(){DbType=DbType.String, ParameterName="ClassId", Value=entity.ClassId},
                new SqlParameter(){DbType=DbType.String, ParameterName="PatriarchId", Value=entity.PatriarchId}
                                };
            DbHelper.ExecuteNonQuery(out id, CommandType.Text, sql, param);
            return id;
        }

        public static void Update(Student entity)
        {
            string sql = string.Format("update edu_student set [Code]=@Code,[Name]=@Name,[Description]=@Description,[ClassId]=@ClassId,[PatriarchId]=@PatriarchId where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Code", Value=entity.Code},
                new SqlParameter(){DbType=DbType.String, ParameterName="Name", Value=entity.Name},
                new SqlParameter(){DbType=DbType.String, ParameterName="Description", Value=entity.Description},
                new SqlParameter(){DbType=DbType.String, ParameterName="ClassId", Value=entity.ClassId},
                new SqlParameter(){DbType=DbType.String, ParameterName="PatriarchId", Value=entity.PatriarchId},
                new SqlParameter(){DbType=DbType.String, ParameterName="Id", Value=entity.Id}
                                };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static void Delete(Student entity)
        {
            throw new NotImplementedException();
        }

        public static void Delete(int id)
        {
            string sql = string.Format("delete from edu_student where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Id", Value=id}
                              };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static DataTable GetStudentsByClassId(int classId)
        {
            string sql = "select Id,Name from edu_Student where classId = @classId order by name ";
            DbParameter cid = new SqlParameter("classId", classId);
            return DbHelper.ExecuteDataset(CommandType.Text, sql, cid).Tables[0];
        }
    }
}
